package demon3;

import demon.IMyArrayList;

public class MySingleLinkedList implements IMyLinkList{
    class NodeList{
        private int val;
        private NodeList next;
        public NodeList(int val){
            this.val= val;
        }
    }
    public NodeList head;
    public NodeList findIndexsubone(int index){
        int count=0;
        NodeList cur=this.head;
        while(count!=index-1){
            cur=cur.next;
        }
        return cur;
    }
    private void check(int index){
        if(index>size()||index<0){
            throw new CheckIndexNotLegalException("index不合法");
        }
    }
    @Override
    public void addFirst(int data) {
        NodeList nodeList= new NodeList(data);
        nodeList.next=head;
        head=nodeList;
    }

    @Override
    public void addLast(int data) {
        NodeList nodeList=new NodeList(data);
        if(this.head==null){
            this.head=nodeList;
        }
        NodeList cur=this.head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=nodeList;
    }

    @Override
    public void addIndex(int index, int data) {
        try{
            check(index);
        }catch (CheckIndexNotLegalException e){
            e.printStackTrace();
        }
        NodeList nodeList=new NodeList(data);
        NodeList cur=findIndexsubone(index);
        if(this.head==null){
            addFirst(data);
        }
        if(index==size()){
            addLast(data);
        }
        nodeList.next=cur.next;
        cur.next=nodeList;

    }

    @Override
    public boolean contains(int key) {
        NodeList cur=this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
        }
        NodeList cur=head;
        while(cur.next!=null){
            if(cur.next.val==key){
                NodeList del=cur;
                cur.next=del.next;
                return;
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(head==null){
            return;
        }
        NodeList prve=head;
        NodeList cur=head.next;

        while(cur!=null){
            if(cur.val==key){
                prve.next=cur.next;
            }else{
                prve=cur;
            }
            cur=cur.next;
        }
        if(head.val==key){
            head=head.next;
        }
    }

    @Override
    public int size() {
        int count =0;
        NodeList cur=head;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        this.head=null;
    }
    @Override
    public void display() {
        NodeList cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }
}
